package com.cdtu.controller;

import com.cdtu.entity.Daily;
import com.cdtu.entity.SysEmployee;
import com.cdtu.entity.TrainingPlan;
import com.cdtu.service.DailyService;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.TrainingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/trainingPlan")
//解决跨域问题
@CrossOrigin
public class TrainingPlanController extends BaseController{
    @Autowired
    private TrainingPlanService trainingPlanService;

    @RequestMapping("/list")
    public ModelAndView dailyList(ModelAndView modelAndView) {
        List<TrainingPlan> list = trainingPlanService.list();
        //返回数据
        modelAndView.addObject("trainingPlanList", list);
        //2.跳转页面;
        modelAndView.setViewName("train/trainingPlan/trainingPlanList");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddUI(ModelAndView modelAndView) {
        //2.跳转页面;
        modelAndView.setViewName("train/trainingPlan/addTrainingPlan");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/add")
    public ModelAndView addDaily(TrainingPlan trainingPlan,ModelAndView modelAndView) {
        trainingPlanService.save(trainingPlan);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/trainingPlan/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{planId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("planId") Long planId) {
        TrainingPlan trainingPlan = trainingPlanService.getById(planId);
        //返回数据
        modelAndView.addObject("trainingPlan", trainingPlan);
        //跳转页面;
        modelAndView.setViewName("train/trainingPlan/updateTrainingPlan");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView,TrainingPlan trainingPlan) {
        System.out.println(trainingPlan);
        trainingPlanService.updateById(trainingPlan);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/trainingPlan/list");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/delete/{planId}")
    public ModelAndView delete(@PathVariable("planId") Long planId, ModelAndView modelAndView) {

        boolean remove = trainingPlanService.removeById(planId);
        modelAndView.setViewName("redirect:/trainingPlan/list");
        return modelAndView;
    }
}
